package com.pipi.sso.core.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.pipi.sso.core.exception.DormServiceException;
import com.pipi.sso.core.service.NoticeService;
import com.pipi.sso.core.enums.CommonStatusEnum;
import com.pipi.sso.core.enums.DormErrorCodeEnum;
import com.pipi.sso.core.model.NoticeBO;
import com.pipi.sso.core.util.BeanUtils;
import com.pipi.sso.core.util.DateUtil;
import com.pipi.sso.dao.manager.NoticeManager;
import com.pipi.sso.dao.model.NoticeDO;
import com.pipi.sso.dao.query.NoticeQuery;
import com.pipi.sso.dao.support.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * All rights Reserved, Designed By www.maihaoche.com
 *
 * @Package com.pipi.sso.core.service.impl
 * @date: 2019/4/15 9:59 AM
 * @Copyright: 2017-2020 www.maihaoche.com Inc. All rights reserved.
 */
@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {

    @Resource
    private NoticeManager noticeManager;

    @Override
    public PageResult<NoticeBO> queryByPaged(NoticeBO noticeBO) {
        try {
            NoticeQuery noticeQuery = buildQuery(noticeBO);
            noticeQuery.setPageNo(noticeBO.getPage());
            noticeQuery.setPageSize(noticeBO.getLimit());
            PageResult<NoticeDO> pageResult = noticeManager.selectByQueryWithPage(noticeQuery);
            if (CollectionUtils.isEmpty(pageResult.getResult())) {
                return PageResult.createPageResult();
            }
            List<NoticeBO> noticeBOS = buildBOs(pageResult.getResult());
            return PageResult.createPageResult(noticeBOS, pageResult.getPageInfo());
        } catch (Exception e) {
            log.error("查询公告失败！noticeBO is {}", JSON.toJSONString(noticeBO), e);
            throw new DormServiceException(DormErrorCodeEnum.QUERY_NOTICE_FAIL);
        }
    }

    @Override
    public List<NoticeBO> queryNoPaged(NoticeBO noticeBO) {
        NoticeQuery noticeQuery = buildQuery(noticeBO);
        try {
            List<NoticeDO> noticeDOS = noticeManager.selectByQuery(noticeQuery);
            if (CollectionUtils.isEmpty(noticeDOS)) {
                return Lists.newArrayList();
            }
            return buildBOs(noticeDOS);
        } catch (Exception e) {
            log.error("查询公告失败！noticeBO is {}", JSON.toJSONString(noticeBO), e);
            throw new DormServiceException(DormErrorCodeEnum.QUERY_NOTICE_FAIL);
        }
    }

    @Override
    public void addNotice(NoticeBO noticeBO) {
        try {
            NoticeDO noticeDO = BeanUtils.createCloneBean(noticeBO, NoticeDO.class);
            noticeDO.setIsDeleted(CommonStatusEnum.NO.getCode());
            noticeManager.insertSelective(noticeDO);
        } catch (Exception e) {
            log.error("添加公告失败！noticeBO is {}", JSON.toJSONString(noticeBO));
            throw new DormServiceException(DormErrorCodeEnum.ADD_NOTICE_FAIL);
        }
    }

    @Override
    public void updateNotice(NoticeBO noticeBO) {
        try {
            NoticeDO noticeDO = BeanUtils.createCloneBean(noticeBO, NoticeDO.class);
            noticeDO.setIsDeleted(CommonStatusEnum.NO.getCode());
            noticeManager.updateByPrimaryKeySelective(noticeDO);
        } catch (Exception e) {
            log.error("更新公告失败！noticeBO is {}", JSON.toJSONString(noticeBO));
            throw new DormServiceException(DormErrorCodeEnum.ADD_NOTICE_FAIL);
        }
    }

    @Override
    public void deleteNotice(List<Long> noticeIds) {
        try {
            NoticeQuery noticeQuery = new NoticeQuery();
            noticeQuery.createCriteria().andNoticeIdIn(noticeIds);

            NoticeDO noticeDO = new NoticeDO();
            noticeDO.setIsDeleted(CommonStatusEnum.YES.getCode());
            noticeDO.setModifier("登录人");
            noticeManager.updateByQuerySelective(noticeDO, noticeQuery);
        } catch (Exception e) {
            log.error("删除公告失败！noticeIds is {}", JSON.toJSONString(noticeIds));
            throw new DormServiceException(DormErrorCodeEnum.DELETE_NOTICE_FAIL);
        }
    }


    private NoticeQuery buildQuery(NoticeBO noticeBO) {
        NoticeQuery noticeQuery = new NoticeQuery();
        noticeQuery.setOrderByClause("gmt_create desc");
        NoticeQuery.Criteria criteria = noticeQuery.createCriteria().andIsDeletedEqualTo(CommonStatusEnum.NO.getCode());
        if (Objects.nonNull(noticeBO)) {
            if (Objects.nonNull(noticeBO.getNoticeId())) {
                criteria.andNoticeIdEqualTo(noticeBO.getNoticeId());
            }
            if (StringUtils.isNotBlank(noticeBO.getNoticeTitle())) {
                criteria.andNoticeTitleLike("%" + noticeBO.getNoticeTitle() + "%");
            }
            if (StringUtils.isNotBlank(noticeBO.getNoticeContent())) {
                criteria.andNoticeContentLike("%" + noticeBO.getNoticeContent() + "%");
            }
            if (StringUtils.isNotBlank(noticeBO.getPublisher())) {
                criteria.andPublisherLike("%" + noticeBO.getPublisher() + "%");
            }
        }
        return noticeQuery;
    }


    private List<NoticeBO> buildBOs(List<NoticeDO> noticeDOS) {
        if (CollectionUtils.isEmpty(noticeDOS)) {
            return null;
        }
        return noticeDOS.stream().map(this::buildBO).collect(Collectors.toList());
    }

    private NoticeBO buildBO(NoticeDO noticeDO) {
        if (Objects.isNull(noticeDO)) {
            return null;
        }
        NoticeBO noticeBO = BeanUtils.createCloneBean(noticeDO, NoticeBO.class);
        noticeBO.setGmtCreateDesc(DateFormatUtils.format(noticeBO.getGmtCreate(), DateUtil.PATTERN_2));
        noticeBO.setGmtModifiedDesc(DateFormatUtils.format(noticeBO.getGmtModified(), DateUtil.PATTERN_2));
        return noticeBO;
    }
}
